package com.woniuxy.yoga.admin.mq;


import com.woniuxy.yoga.admin.dao.mapper.AdminAccountMapper;
import com.woniuxy.yoga.admin.dao.mapper.AdminLoginLogMapper;
import com.woniuxy.yoga.admin.dao.model.AdminAccount;
import com.woniuxy.yoga.admin.dao.model.AdminLoginLog;
import com.woniuxy.yoga.admin.model.exception.AdminException;
import com.woniuxy.yoga.admin.model.exception.AdminExceptionCode;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.ws.soap.Addressing;


@Component
public class CreateAdminLogConsumer {


    @Autowired
    private AdminLoginLogMapper adminLoginLogMapper;

    @Autowired
    private AdminAccountMapper adminAccountMapper;

    /**
     * 生成管理员登录日志
     * @param adminLoginLog
     * @throws Exception
     */
    @RabbitListener(queuesToDeclare = {@Queue(name = "create-adminLoginLog")})
    @RabbitHandler
    public void consumer(AdminLoginLog adminLoginLog) throws Exception{
        AdminAccount adminAccount = adminAccountMapper.selectById(adminLoginLog.getAccountId());
        if (adminAccount==null) {
            throw new AdminException(AdminExceptionCode.ADMIN_EXCEPTION_CODE_CREATELOG_FAIL);
        }
        adminLoginLogMapper.insert(adminLoginLog);
    }
}
